package com.book.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Value("${app.file.upload-path}")
    private String uploadPath;
    @Value("${app.file.prefix}")
    private String filePrefix;
    /**
     * 资源映射
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 动态获取项目根目录并拼接上传路径
        String projectRoot = System.getProperty("user.dir");
        String fullPath = new File(projectRoot, uploadPath).getAbsolutePath() + File.separator;

        // 注册资源映射
        registry.addResourceHandler(filePrefix + "/**")
                .addResourceLocations("file:" + fullPath);
    }
}
